package com.example.baseweather.utils;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.media.MediaMetadataRetriever;
import android.os.Build;
import android.os.Environment;
import android.text.format.DateFormat;
import android.text.format.DateUtils;
import android.view.View;
import android.view.WindowInsets;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;

import com.example.baseweather.domain.VideoInfoBean;

import java.io.File;
import java.io.FilenameFilter;
import java.math.BigDecimal;

public class CommonUtils {


    public static void showToastShort(String message, Context context) {
        Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
    }

    public static void showToastLong(String message, Context context) {
        Toast.makeText(context, message, Toast.LENGTH_LONG).show();
    }

    public static <T extends Activity> void startAc(Context context, Class<T> clazz) {
        Intent intent = new Intent(context, clazz);
        context.startActivity(intent);
    }


    public static File[] getVideoPath() {
        String path = Environment.getExternalStorageDirectory().getPath() + File.separator + "Download";
        File file = new File(path);
        File[] files = file.listFiles(new FilenameFilter() {
            @Override
            public boolean accept(File dir, String name) {
                return name.endsWith(".mp4") || name.endsWith(".fav");
            }
        });
        return files;
    }


    public static Bitmap getVideoFrame(@NonNull String videoPath, int frameSeconds
    ) {
        try (MediaMetadataRetriever mediaMetadataRetriever = new MediaMetadataRetriever()) {
            mediaMetadataRetriever.setDataSource(videoPath);
            Bitmap bitmap = mediaMetadataRetriever.getFrameAtTime(frameSeconds * 1000 * 1000, MediaMetadataRetriever.OPTION_CLOSEST);
            return bitmap;
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static VideoInfoBean getVideoInfo(File file) {
        String absolutePath = file.getAbsolutePath();
        String[] split = absolutePath.split("/");
        String videoName = split[split.length - 1].replace(".mp4", "");
        Bitmap videoBitmap = CommonUtils.getVideoFrame(file.getPath(), 1);
        String playInfo = new StringBuilder().append("上次看到").append("0分0秒").toString();
        return new VideoInfoBean(videoName, videoBitmap, playInfo, absolutePath);
    }


    public static int dpToPx(Context context, int dp) {
        float density = context.getResources().getDisplayMetrics().density;
        return ((int) (dp * density));
    }


    public static String formatDate(long milliseconds) {
        double totalSeconds = milliseconds / 1000.0;
        int minute = (int) totalSeconds / 60;
        int second = (int) Math.round(totalSeconds % 60);
        return minute + ":" + second;
    }


    public static void showFullScreen(AppCompatActivity ac, View view) {
        ActionBar actionBar = ac.getSupportActionBar();
        if (actionBar != null) {
            actionBar.hide();
        }
        if (Build.VERSION.SDK_INT >= 30) {
            view.getWindowInsetsController().hide(
                    WindowInsets.Type.statusBars() | WindowInsets.Type.navigationBars());
        } else {
            // Note that some of these constants are new as of API 16 (Jelly Bean)
            // and API 19 (KitKat). It is safe to use them, as they are inlined
            // at compile-time and do nothing on earlier devices.
            view.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE
                    | View.SYSTEM_UI_FLAG_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                    | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
        }
    }

}
